package org.jeecg.modules.message.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.bid.entity.BidManagement;
import org.jeecg.modules.bid.service.IBidManagementService;
import org.jeecg.modules.lottery.entity.LotteryManagement;
import org.jeecg.modules.lottery.service.ILotteryManagementService;
import org.jeecg.modules.message.Vo.MessageVo;
import org.jeecg.modules.message.dto.MessageSeachBySTRequest;
import org.jeecg.modules.message.entity.ProcurementMessage;
import org.jeecg.modules.message.service.IProcurementMessageService;
import org.jeecg.modules.supplier.service.IProcUserSupplierRelationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags="通知33")
@RestController
@RequestMapping("/procurement/message")
@Slf4j
public class ProcurementMessageController extends JeecgController<ProcurementMessage, IProcurementMessageService> {

    @Autowired
    private IProcurementMessageService procurementMessageService;

    @Resource
    private IProcUserSupplierRelationService procUserSupplierRelationService;

    @Autowired
    private IBidManagementService bidManagementService;
    @Autowired
    private ILotteryManagementService lotteryManagementService;
    /**
     * 分页列表查询
     */
    @AutoLog(value = "采购消息-分页列表查询")
    @ApiOperation(value="采购消息-分页列表查询", notes="采购消息-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String receiverId = sysUser.getId();
        Page<ProcurementMessage> page = new Page<>(pageNo, pageSize);
        // 使用QueryWrapper添加查询条件，确保只返回当前用户的消息
        QueryWrapper<ProcurementMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("receiver_id", receiverId);
        // 按创建时间倒序排列，最新消息在前
        queryWrapper.orderByDesc("create_time");
        page = procurementMessageService.page(page, queryWrapper);
        return Result.OK(page);
    }

    /**
     * 添加消息
     */
    @AutoLog(value = "采购消息-添加")
    @ApiOperation(value="采购消息-添加", notes="采购消息-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody ProcurementMessage procurementMessage) {
        // 参数校验
        if (procurementMessage == null) {
            return Result.error("消息内容不能为空");
        }
        if (procurementMessage.getTitle() == null || procurementMessage.getTitle().trim().isEmpty()) {
            return Result.error("消息标题不能为空");
        }
        if (procurementMessage.getContent() == null || procurementMessage.getContent().trim().isEmpty()) {
            return Result.error("消息内容不能为空");
        }
        if (procurementMessage.getReceiver() == null || procurementMessage.getReceiver().trim().isEmpty()) {
            return Result.error("接收人不能为空");
        }
        if (procurementMessage.getReceiverId() == null || procurementMessage.getReceiverId().trim().isEmpty()){
            return Result.error("接收人id不能为空");
        }

        procurementMessageService.save(procurementMessage);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑消息
     */
    @AutoLog(value = "采购消息-编辑")
    @ApiOperation(value="采购消息-编辑", notes="采购消息-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody ProcurementMessage procurementMessage) {
        // 参数校验
        if (procurementMessage == null) {
            return Result.error("消息内容不能为空");
        }
        if (procurementMessage.getId() == null || procurementMessage.getId().trim().isEmpty()) {
            return Result.error("消息ID不能为空");
        }

        ProcurementMessage procurementMessage1 = procurementMessageService.getById(procurementMessage.getId());
        if (procurementMessage1 == null) {
            return Result.error("该通知不存在");
        }
        procurementMessageService.updateById(procurementMessage);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除消息
     */
    @AutoLog(value = "采购消息-通过id删除")
    @ApiOperation(value="采购消息-通过id删除", notes="采购消息-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id") String id) {
        // 参数校验
        if (id == null || id.trim().isEmpty()) {
            return Result.error("消息ID不能为空");
        }

        procurementMessageService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除消息
     */
    @AutoLog(value = "采购消息-批量删除")
    @ApiOperation(value="采购消息-批量删除", notes="采购消息-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids") String ids) {
        // 参数校验
        if (ids == null || ids.trim().isEmpty()) {
            return Result.error("ID列表不能为空");
        }
        List<String> idList = Arrays.asList(ids.split(","));
        if (idList.isEmpty()) {
            return Result.error("ID列表不能为空");
        }

        boolean result = procurementMessageService.removeByIds(idList);
        if (result) {
            return Result.OK("批量删除成功!");
        } else {
            return Result.error("批量删除失败!");
        }
    }

    /**
     * 通过id查询消息
     */
    @AutoLog(value = "采购消息-通过id查询")
    @ApiOperation(value="采购消息-通过id查询", notes="采购消息-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id") String id) {
        // 参数校验
        if (id == null || id.trim().isEmpty()) {
            return Result.error("消息ID不能为空");
        }
        MessageVo messageVo = new MessageVo();
        ProcurementMessage procurementMessage = procurementMessageService.getById(id);
//lottery查找LotteryManagement
        //bid查找BidManagement
        if (procurementMessage == null) {
            return Result.error("未找到对应数据");
        }
        //把procurementMessage复制给messageVo
        BeanUtils.copyProperties(procurementMessage, messageVo);

        //如果businessType=lottery/bid 则通过businessId查找对应的实体
        if(procurementMessage.getBusinessType().equals("bid")){
            BidManagement bidManagement = bidManagementService.getById(procurementMessage.getBusinessId());
            messageVo.setBidManagement(bidManagement);
        }
        if(procurementMessage.getBusinessType().equals("lottery")){
            LotteryManagement lotteryManagement =lotteryManagementService.getById(procurementMessage.getBusinessId());
            messageVo.setLotteryManagement(lotteryManagement);

        }

        return Result.OK(messageVo);
    }

    /**
     * 获取未读消息数量
     */
    @AutoLog(value = "采购消息-获取未读/已读消息")
    @ApiOperation(value="采购消息-获取未读消息", notes="采购消息-获取未读/已读消息")
    @PostMapping(value = "/unread")
    public Result<?> getUnreadMessages(@RequestParam(name = "readStatus") String readStatus) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//        // 参数校验
//        if (receiver == null || receiver.trim().isEmpty()) {
//            return Result.error("接收人不能为空");
//        }
//        if (receiverId == null ||receiverId.trim().isEmpty()){
//            return Result.error("接收人id不能为空");
//        }

        List<ProcurementMessage> messages = procurementMessageService.getUnreadMessages(sysUser.getId(),readStatus);
        return Result.OK(messages);
    }
    /**
     * 标记消息已读
     */
    @AutoLog(value = "采购消息-标记已读1/未读0")
    @ApiOperation(value="采购消息-标记已读1/未读0", notes="采购消息-标记已读1/未读0")
    @PostMapping(value = "/markRead")
    public Result<?> markRead(@RequestParam(name="messageId") String messageId,
                              @RequestParam(name = "readStatus") String readStatus) {
        // 参数校验
        if (messageId == null || messageId.trim().isEmpty()) {
            return Result.error("消息ID不能为空");
        }

        boolean result = procurementMessageService.updateReadStatus(messageId, readStatus);
        if (result) {
            return Result.OK("标记已读成功!");
        } else {
            return Result.error("标记已读失败!");
        }
    }

    /**
     * 批量标记消息已读
     */
    @AutoLog(value = "采购消息-批量标记已读1/未读0")
    @ApiOperation(value="采购消息-批量标记已读1/未读0", notes="采购消息-批量标记已读1/未读0")
    @PostMapping(value = "/batchMarkRead")
    public Result<?> batchMarkRead(@RequestParam(name="messageIds") String messageIds,
                                   @RequestParam(name = "readStatus") String readStatus) {
        // 参数校验
        if (messageIds == null || messageIds.trim().isEmpty()) {
            return Result.error("消息ID列表不能为空");
        }
        List<String> idList = Arrays.asList(messageIds.split(","));
        if (idList.isEmpty()) {
            return Result.error("消息ID列表不能为空");
        }
        boolean result = procurementMessageService.batchUpdateReadStatus(idList, readStatus);
        if (result) {
            return Result.OK("批量标记已读成功!");
        } else {
            return Result.error("批量标记已读失败!");
        }
    }

//    /**
//     * 发送采购计划审核通知（审核通过自动发送）
//     */
//    @AutoLog(value = "采购消息-发送采购计划审核通知（审核通过自动发送）")
//    @ApiOperation(value="采购消息-发送采购计划审核通知（审核通过自动发送）", notes="采购消息-发送采购计划审核通知（审核通过自动发送）")
//    @PostMapping(value = "/sendPlanAuditNotification")
//    public Result<?> sendPlanAuditNotification(@RequestParam(name="purchaseId") String purchaseId,
//                                               @RequestParam(name="Title") String Title,
//                                               @RequestParam(name="auditStatus") String auditStatus,
//                                               @RequestParam(name="auditOpinion", required = false) String auditOpinion,
//                                               @RequestParam(name = "receiverId") String receiverId,
//                                               @RequestParam(name = "receiver") String receiver){
//        // 参数校验
//        if (purchaseId == null || purchaseId.trim().isEmpty()) {
//            return Result.error("采购计划ID不能为空");
//        }
//        if (Title == null || Title.trim().isEmpty()) {
//            return Result.error("通知标题不能为空");
//        }
//        if (auditStatus == null || auditStatus.trim().isEmpty()) {
//            return Result.error("审核状态不能为空");
//        }
//        if (receiverId == null ||receiverId.trim().isEmpty()){
//            return Result.error("接收人id不能为空");
//        }
//
//        boolean result = procurementMessageService.sendPurchasePlanNotice(purchaseId, Title, auditStatus,
//                auditOpinion,receiverId,receiver);
//        if (result) {
//            return Result.OK("发送采购计划审核通知成功!");
//        } else {
//            return Result.error("发送采购计划审核通知失败!");
//        }
//    }

    /**
     * 发送供应商审核通知（审核通过自动发送）
     */
    @AutoLog(value = "供应商消息-发送供应商审核通知（审核通过自动发送）")
    @ApiOperation(value="供应商消息-发送供应商审核通知（审核通过自动发送）", notes="供应商消息-发送供应商审核通知（审核通过自动发送）")
    @PostMapping(value = "/sendSupplierAuditNotification")
    public Result<?> sendSupplierAuditNotification(@RequestParam(name="supplierId") String supplierId,
                                                   @RequestParam(name="supplierName") String supplierName,
                                                   @RequestParam(name="auditStatus") String auditStatus,
                                                   @RequestParam(name="auditOpinion", required = false) String auditOpinion,
                                                   @RequestParam(name = "receiverId") String receiverId,
                                                   @RequestParam(name = "receiver") String receiver){

        // 参数校验
        if (supplierId == null || supplierId.trim().isEmpty()) {
            return Result.error("供应商ID不能为空");
        }
        if (supplierName == null || supplierName.trim().isEmpty()) {
            return Result.error("供应商名称不能为空");
        }
        if (auditStatus == null || auditStatus.trim().isEmpty()) {
            return Result.error("审核状态不能为空");
        }
        if (receiverId == null ||receiverId.trim().isEmpty()){
            return Result.error("接收人id不能为空");
        }
        boolean result = procurementMessageService.sendSupplierAuditNotice(supplierId, supplierName, auditStatus,
                auditOpinion,receiverId,receiver);
        if (result) {
            return Result.OK("发送供应商审核通知成功!");
        } else {
            return Result.error("发送供应商审核通知失败!");
        }
    }

    /**
     * 发送投标截止提醒（距离截止时间少于两天，自动提醒供应商）
     */
    @AutoLog(value = "投标消息-发送投标即将截止提醒")
    @ApiOperation(value="采购消息-发送投标即将截止提醒", notes="采购消息-发送投标即将截止提醒")
    @PostMapping(value = "/sendBiddingDeadlineReminder")
    public Result<?> sendBiddingDeadlineReminder(@RequestParam(name="bidId") String bidId,
                                                 @RequestParam(name="bidTitle") String bidTitle,
                                                 @RequestParam(name="deadlineTime") String deadlineTime,
                                                 @RequestParam(name = "receiverId") String receiverId,
                                                 @RequestParam(name = "receiver") String receiver) {
        // 参数校验
        if (bidId == null || bidId.trim().isEmpty()) {
            return Result.error("投标ID不能为空");
        }
        if (bidTitle == null || bidTitle.trim().isEmpty()) {
            return Result.error("投标标题不能为空");
        }
        if (deadlineTime == null || deadlineTime.trim().isEmpty()) {
            return Result.error("截止时间不能为空");
        }
        if (receiverId == null ||receiverId.trim().isEmpty()){
            return Result.error("接收人id不能为空");
        }

        boolean result = procurementMessageService.sendBiddingDeadlineReminder(bidId, bidTitle, deadlineTime,receiverId,receiver);
        if (result) {
            return Result.OK("发送投标截止提醒成功!");
        } else {
            return Result.error("发送投标截止提醒失败!");
        }
    }

    /**
     * 发送投标已经截止提醒（如果在24小时前截止，自动提醒供应商）
     */
    @AutoLog(value = "投标消息-发送投标已截止提醒")
    @ApiOperation(value="采购消息-发送投标已截止提醒", notes="采购消息-发送投标已截止提醒")
    @PostMapping(value = "/afBendBiddingDeadlineReminder")
    public Result<?> afSendBiddingDeadlineReminder(@RequestParam(name="bidId") String bidId,
                                                 @RequestParam(name="bidTitle") String bidTitle,
                                                 @RequestParam(name="deadlineTime") String deadlineTime,
                                                 @RequestParam(name = "receiverId") String receiverId,
                                                   @RequestParam(name = "receiver") String receiver) {
        // 参数校验
        if (bidId == null || bidId.trim().isEmpty()) {
            return Result.error("投标ID不能为空");
        }
        if (bidTitle == null || bidTitle.trim().isEmpty()) {
            return Result.error("投标标题不能为空");
        }
        if (deadlineTime == null || deadlineTime.trim().isEmpty()) {
            return Result.error("截止时间不能为空");
        }
        if (receiverId == null ||receiverId.trim().isEmpty()){
            return Result.error("接收人id不能为空");
        }
        boolean result = procurementMessageService.afSendBiddingDeadlineReminder(bidId, bidTitle, deadlineTime,receiverId,receiver);
        if (result) {
            return Result.OK("发送投标截止提醒成功!");
        } else {
            return Result.error("发送投标截止提醒失败!");
        }
    }

    /**
     * 发送开标通知
     */
    @AutoLog(value = "投标消息-发送开标通知")
    @ApiOperation(value="采购消息-发送开标通知", notes="采购消息-发送开标通知")
    @PostMapping(value = "/sendBidOpeningNotification")
    public Result<?> sendBidOpeningNotification(@RequestParam(name="bidId") String bidId,
                                                @RequestParam(name="bidTitle") String bidTitle,
                                                @RequestParam(name="openTime") String openTime,
                                                @RequestParam(name = "receiverId") String receiverId,
                                                @RequestParam(name = "receiver") String receiver) {
        // 参数校验
        if (bidId == null || bidId.trim().isEmpty()) {
            return Result.error("投标ID不能为空");
        }
        if (bidTitle == null || bidTitle.trim().isEmpty()) {
            return Result.error("投标标题不能为空");
        }
        if (openTime == null || openTime.trim().isEmpty()) {
            return Result.error("开标时间不能为空");
        }
        if (receiverId == null ||receiverId.trim().isEmpty()){
            return Result.error("接收人id不能为空");
        }

        boolean result = procurementMessageService.sendBidOpeningNotice(bidId, bidTitle, openTime,receiverId,receiver);
        if (result) {
            return Result.OK("发送开标通知成功!");
        } else {
            return Result.error("发送开标通知失败!");
        }
    }

//    /**
//     * 发送中标结果通知(中标后自动发送)
//     */
//    @AutoLog(value = "投标消息-发送中标结果通知(中标后自动发送)")
//    @ApiOperation(value="投标消息-发送中标结果通知(中标后自动发送)", notes="投标消息-发送中标结果通知(中标后自动发送)")
//    @PostMapping(value = "/sendWinningResultNotification")
//    public Result<?> sendWinningResultNotification(@RequestParam(name="bidId") String bidId,
//                                                   @RequestParam(name="bidTitle") String bidTitle,
//                                                   @RequestParam(name = "SupplierId") String supplierId){
//        // 参数校验
//        if (bidId == null || bidId.trim().isEmpty()) {
//            return Result.error("投标ID不能为空");
//        }
//        if (bidTitle == null || bidTitle.trim().isEmpty()) {
//            return Result.error("投标标题不能为空");
//        }
//
//
//        boolean result = procurementMessageService.sendWinningResultNotice(bidId, bidTitle,receiverId,receiver);
//        if (result) {
//            return Result.OK("发送中标结果通知成功!");
//        } else {
//            return Result.error("发送中标结果通知失败!");
//        }
//    }

    /**
     * 发送流标通知
     */
    @AutoLog(value = "投标消息-发送流标通知")
    @ApiOperation(value="投标消息-发送流标通知", notes="投标消息-发送流标通知")
    @PostMapping(value = "/sendBidFailureNotification")
    public Result<?> sendBidFailureNotification(@RequestParam(name="bidId") String bidId,
                                                @RequestParam(name="bidTitle") String bidTitle,
                                                @RequestParam(name="reason", required = false) String reason,
                                                @RequestParam(name = "receiverId") String receiverId,
                                                @RequestParam(name = "receiver") String receiver) {
        // 参数校验
        if (bidId == null || bidId.trim().isEmpty()) {
            return Result.error("投标ID不能为空");
        }
        if (bidTitle == null || bidTitle.trim().isEmpty()) {
            return Result.error("投标标题不能为空");
        }
        if (receiverId == null ||receiverId.trim().isEmpty()){
            return Result.error("接收人id不能为空");
        }

        boolean result = procurementMessageService.sendBidFailureNotice(bidId, bidTitle, reason,receiverId,receiver);
        if (result) {
            return Result.OK("发送流标通知成功!");
        } else {
            return Result.error("发送流标通知失败!");
        }
    }

    /**
     * 批量发送消息
     */
    @AutoLog(value = "投标消息-批量发送消息")
    @ApiOperation(value="投标消息-批量发送消息", notes="投标消息-批量发送消息")
    @PostMapping(value = "/sendBatchMessage")
    public Result<?> sendBatchMessage(@RequestParam(name="title") String title,
                                      @RequestParam(name="content") String content,
                                      @RequestParam(name="messageType") String messageType,
                                      @RequestParam(name="businessType", required = false) String businessType,
                                      @RequestParam(name="businessId", required = false) String businessId,
                                      @RequestParam(name="receiverIds") String receiverIds,
                                      @RequestParam(name="receivers") String receivers,
                                      @RequestParam(name="sender") String sender,
                                      @RequestParam(name="senderId", required = false) String senderId) {
        // 1. 校验参数
        if (title == null || title.trim().isEmpty()) {
            return Result.error("标题不能为空");
        }
        if (content == null || content.trim().isEmpty()) {
            return Result.error("内容不能为空");
        }
        if (messageType == null || messageType.trim().isEmpty()) {
            return Result.error("消息类型不能为空");
        }
        if (receiverIds == null || receiverIds.trim().isEmpty()) {
            return Result.error("接收人ID列表不能为空");
        }
        if (receivers == null || receivers.trim().isEmpty()) {
            return Result.error("接收人名字列表不能为空");
        }
        if (sender == null || sender.trim().isEmpty()) {
            return Result.error("发送人不能为空");
        }


        // 3. 处理接收人ID列表
        List<String> receiverIdList = Arrays.stream(receiverIds.split(","))
                .map(String::trim)
                .filter(id -> !id.isEmpty())
                .collect(Collectors.toList());

        // 4. 处理接收人名字列表
        List<String> receiverList = Arrays.stream(receivers.split(","))
                .map(String::trim)
                .filter(name -> !name.isEmpty())
                .collect(Collectors.toList());

        // 5. 验证接收人名字列表和ID列表数量是否匹配
        if (receiverList.size() != receiverIdList.size()) {
            return Result.error("接收人名字列表和ID列表数量不匹配，请检查参数");
        }

        if (receiverIdList.isEmpty()) {
            return Result.error("接收人ID列表不能为空");
        }

        // 7. 调用服务层发送消息
        boolean result = procurementMessageService.sendBatchMessage(
                title, content, messageType, businessType,
                businessId, receiverIdList);

        if (result) {
            return Result.OK("data", "批量发送消息成功!");
        } else {
            return Result.error("批量发送消息部分或全部失败，请检查日志");
        }

    }

    /**
     * 根据消息类型获取消息
     */
    @AutoLog(value = "消息-根据类型获取消息")
    @ApiOperation(value="消息-根据类型获取消息", notes="消息-根据类型获取消息")
    @GetMapping(value = "/getByType")
    public Result<?> getMessagesByType(@RequestParam(name="messageType") String messageType) {
        // 参数校验
        if (messageType == null || messageType.trim().isEmpty()) {
            return Result.error("消息类型不能为空");
        }
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        List<ProcurementMessage> messages = procurementMessageService.getMessagesByType(messageType,sysUser.getId());
        return Result.OK(messages);
    }

    /**
     * 根据业务获取消息
     */
    @AutoLog(value = "消息-根据业务获取消息")
    @ApiOperation(value="消息-根据业务获取消息", notes="消息-根据业务获取消息")
    @GetMapping(value = "/getByBusiness")
    public Result<?> getMessagesByBusiness(@RequestParam(name="businessType") String businessType,
                                           @RequestParam(name="businessId") String businessId) {
        // 参数校验
        if (businessType == null || businessType.trim().isEmpty()) {
            return Result.error("业务类型不能为空");
        }
        if (businessId == null || businessId.trim().isEmpty()) {
            return Result.error("业务ID不能为空");
        }
        List<ProcurementMessage> messages = procurementMessageService.getMessagesByBusiness(businessType, businessId);
        return Result.OK(messages);
    }

    @PostMapping("/sendLotteryWinningMessage")
    public Result<?> sendLotteryWinningMessage(@RequestParam(name="supplierId") String supplierId,
                                                @RequestParam(name="businessId")String businessId) {
        // 参数校验
        if (supplierId == null || supplierId.trim().isEmpty()) {
            return Result.error("供应商ID不能为空");
        }
        procurementMessageService.sendLotteryWinningMessage(supplierId,businessId);

            return Result.OK("消息发送成功");
    }
    /**
     * 根据发送类型获取消息
     */
    @AutoLog(value = "消息-根据发送类型获取消息")
    @ApiOperation(value="消息-根据发送类型获取消息", notes="消息-根据发送类型获取消息")
    @GetMapping("/getMessagePageBySendType")
    public Result<?> getMessagePageBySendType(MessageSeachBySTRequest queryRequest) {
// 如果queryRequest为null，创建默认实例

        // 设置默认分页参数
        if (queryRequest.getPageNum() == null || queryRequest.getPageNum() <= 0) {
            queryRequest.setPageNum(1);
        }
        if (queryRequest.getPageSize() == null || queryRequest.getPageSize() <= 0) {
            queryRequest.setPageSize(10);
        }
        // 创建查询条件
        Page<ProcurementMessage> page = new Page<>(queryRequest.getPageNum(), queryRequest.getPageSize());
        QueryWrapper<ProcurementMessage> queryWrapper = new QueryWrapper<>();
        // 按发送类型查询
        if(queryRequest.getSendType()!=null &&!queryRequest.getSendType().isEmpty()){
            queryWrapper.eq("send_type", queryRequest.getSendType());
        }

        // 按创建时间倒序排列
        queryWrapper.orderByDesc("create_time");
        try {
            Page<ProcurementMessage> MessagePageList = procurementMessageService.page(page, queryWrapper);
            if (page.getTotal() == 0){
                return Result.OK("未查询到符合条件的消息", MessagePageList);
            }
            return Result.OK(MessagePageList);
        }catch (Exception e) {
            log.error("查询消息失败", e);
            return Result.error("查询消息失败：" + e.getMessage());
        }
    }

    //通过id查询ProcurementMessage

}
